<template>
  <div class="tar-bar-item" @click="itemClick">
    <div v-if="!isActive"><slot name="item-icon"></slot></div>
    <div v-else><slot name="item-icon-active"></slot></div>
    <div :style="activeStyle"><slot name="item-text"></slot></div>
  </div>
</template>
<script>
export default {
  name: "TarBarItem",
  data() {
    return {
      // isActive: true,
    };
  },
  props: {
    path: String,
    activeColor: {
      type: String,
      default: "red",
    },
  },
  methods: {
    itemClick() {
      this.$router.push(this.path);
    },
  },
  computed: {
    isActive() {
      // indexOf()返回某个指定的字符串值在字符串中首次出现的位置。  == -1 说明不存在
      // return this.$route.path.indexOf(this.path) !== -1;
      // 和上面一样
      return this.$route.path == this.path;
    },
    // 根据传进来的值activeColor 给文字动态绑定style
    activeStyle() {
      return this.isActive ? { color: this.activeColor } : {};
    },
  },
};
</script>
<style scoped>
.tar-bar-item {
  height: 49px;
  flex: 1;
  text-align: center;
  font-size: 14px;
}
.tar-bar-item img {
  width: 24px;
  height: 24px;
  margin-top: 3px;

  /* 去掉图片与文字默认的间隔3px */
  vertical-align: middle;

  margin-bottom: 2px;
}
/* .active {
  color: red;
} */
</style>
